import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

public class GameFrame extends JFrame {
    //定义窗口大小
    final int frameWidth = 800;
    final int frameHeight = 600;
    final int step = 30;
    //定义蛇蛇身的对象
    public static int score = 0;
    public static int state = 0;
    Image cacheImage = null;
    ArrayList<SnakeBody> snakeBodies = new ArrayList<>();
    Random random = new Random();
    FoodObj foodObj = getRandomFood();
    public FoodObj getRandomFood(){
        int lineNum = frameHeight/step;
        int x = random.nextInt(lineNum) * step;
        int y = (random.nextInt(lineNum - 1) + 1) * step;
        return new FoodObj(x,y,GameUtils.foodImg,this);
    }

    public void launch(){
        //设置窗口标题
        this.setTitle("贪吃蛇");
        //设置窗口大小
        this.setSize(frameWidth, frameHeight);
        //设置窗口相对位置
        this.setLocationRelativeTo(null);
        //设置窗口可见性
        this.setVisible(true);
        //设置窗口关闭操作  JFrame.EXIT_ON_CLOSE 关闭窗口，程序退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //初始化蛇身体
        snakeBodies.add(new SnakeBody(0,570,GameUtils.bodyImg,this));
        snakeBodies.add(new SnakeBody(-30,570,GameUtils.bodyImg,this));

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE){
                    switch (state){
                        case 0:
                        case 2:
                            state = 1;break;
                        case 1:
                            state = 2;break;
//                        case 3:
//                            state = 1;
//                            foodObj = get
                        default:break;
                    }
                }
            }
        });

        while (true){
            if (state == 1){
                repaint();
            }

            try {
                Thread.sleep(200);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    public SnakeHead snakeHead = new SnakeHead(30,570,GameUtils.rightImg,this);

    public void paint(Graphics graphics){
        if (cacheImage == null){
            cacheImage = this.createImage(frameWidth,frameHeight);
        }
        Graphics oldGraphics = graphics;
        graphics = cacheImage.getGraphics();
        graphics.setColor(Color.DARK_GRAY);
        graphics.fillRect(0,0,frameWidth,frameHeight);
        //设置线条颜色
        graphics.setColor(Color.gray);
        int lineNum = frameHeight / step; //划线的数量
        for (int i = 0; i < lineNum; i++) {
            //画横线
            graphics.drawLine(0,i*step,frameHeight,i*step);
            //画竖线
            graphics.drawLine(i*step,0,i*step,frameHeight);
        }
        //绘制蛇身
        for(int i = snakeBodies.size() - 1; i >= 0; i--){
            snakeBodies.get(i).paintSelf(graphics);
        }
        //绘制蛇头
        snakeHead.paintSelf(graphics);
        foodObj.paintSelf(graphics);

        oldGraphics.drawImage(cacheImage,0,0,null);
    }

    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame();
        gameFrame.launch();
    }
}
